<?php

namespace app\backstage\controller;


use app\model\Upload;
use gmars\tp5\Qiniu;
use Qcloud\Cos\Client;
class Index extends Base {
    public function index() {
        return json(['welcome']);
    }

    public function upload() {
        $name = $_FILES['file']['name'];
        $tmp_name = $_FILES['file']['tmp_name'];
        $error = $_FILES['file']['error'];
        //过滤错误
        if ($error) {
            switch ($error) {
                case 1 :
                    $error_message = '您上传的文件超过了PHP.INI配置文件中UPLOAD_MAX-FILESIZE的大小';
                    break;
                case 2 :
                    $error_message = '您上传的文件超过了PHP.INI配置文件中的post_max_size的大小';
                    break;
                case 3 :
                    $error_message = '文件只被部分上传';
                    break;
                case 4 :
                    $error_message = '文件不能为空';
                    break;
                default :
                    $error_message = '未知错误';
            }
            die($error_message);
        }
        $arr_name = explode('.', $name);
        $hz = array_pop($arr_name);
        $path = ROOT_PATH . 'public' .DS. 'upload' .DS. date('Ymd');
        $new_name =  md5(time() . uniqid()) . '.' . $hz;
        $target_file = $path . DS . $new_name;
        if (in_array($hz,config('apiAdmin.UPLOAD_EXT'))) {
            if(!is_dir($path)){
                if(!mkdir($path, 0777, true)){
                    return $this->buildFailed(-1, '上传失败');
                }
            }
            if(move_uploaded_file($_FILES['file']['tmp_name'], $target_file)){
                return $this->buildSuccess([
                    'fileName' => $new_name,
                    'fileUrl'  => Request()->domain() . '/' . 'upload/' . date('Ymd') . '/' .$new_name
                ]);
            } else {
                return $this->buildFailed(-1, '上传失败');
            }

        } else {
            return $this->buildFailed(-1, '上传失败','上传文件后缀不允许');
        }

    }

    public function up(){
        $class = new Qiniu();
        $flag = $class->upload(''.time().".jpg");
//        $flag = $class->upload("1591258201.jpg");
        return $flag;
    }

    public function Qrcode(){
        $id = $this->request->post()['value'];
        $QRcode = getQrcode(['masterID' => $id]);
        return $this->buildSuccess($QRcode);
    }

    public function AgentQrcode(){
        $id = $this->request->post()['value'];
        $url = config('apiAdmin.AGENT_URL').$id;
        $QRcode = getQrcode($url,'',1);
        return $this->buildSuccess($QRcode);
    }

}
